

package com.hazelcast.query.impl.getters;

import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import java.util.HashMap;
import java.util.Map;

/**
 * A {@link GetterCache} that uses simple, non-concurrent HashMaps.
 */
@NotThreadSafe
class SimpleGetterCache implements GetterCache {
    private final Map<Class<?>, Map<String, Getter>> cache = new HashMap<>();

    @Nullable
    @Override
    public Getter getGetter(Class<?> clazz, String attributeName) {
        Map<String, Getter> getterMapForClass = cache.get(clazz);
        if (getterMapForClass == null) {
            return null;
        }
        return getterMapForClass.get(attributeName);
    }

    @Override
    public Getter putGetter(Class<?> clazz, String attributeName, Getter getter) {
        return cache.computeIfAbsent(clazz, aClass -> new HashMap<>()).putIfAbsent(attributeName, getter);
    }
}
